﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Xml;
using System.Configuration;

namespace Interface.HeThong
{
    public partial class SQLConnection : Form
    {
        public SQLConnection()
        {
            InitializeComponent();
        }       
        
        public void A()
        {
            txtDBName.Text = "";
        }

        private void btnCon_Click(object sender, EventArgs e)
        {
            connect();
        }

        public void connect()
        {
            try
            {
                //Constructing connection string from the inputs
                StringBuilder Con = new StringBuilder("Data Source=");
                Con.Append(txtServerName.Text);
                Con.Append(";Initial Catalog=");
                Con.Append(txtDBName.Text);
                Con.Append(";Integrated Security=SSPI;");
                string strCon = Con.ToString();
                updateConfigFile(strCon);
                //Create new sql connection
                SqlConnection conn = new SqlConnection();
                //to refresh connection string each time else it will use previous connection string
                ConfigurationManager.RefreshSection("connectionStrings");
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["QLHSConn"].ToString();
                //To check new connection string is working or not
                SqlDataAdapter da = new SqlDataAdapter("select * from Users", conn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    MessageBox.Show("Kết nối cơ sở dữ liệu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    Login login = new Login();
                    login.Activate();
                    login.Show();
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show("Kết nối cơ sở dữ liệu thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        public void updateConfigFile(string con)
        {
            //updating config file
            XmlDocument XmlDoc = new XmlDocument();
            //Loading the Config file
            XmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
            foreach (XmlElement xElement in XmlDoc.DocumentElement)
            {
                if (xElement.Name == "connectionStrings")
                {
                    //setting the coonection string
                    xElement.FirstChild.Attributes[2].Value = con;
                }
            }
            //writing the connection string in config file
            XmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
        }

        private void txtDBName_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                connect();
        }

        
        
    }
}
